package com.cn.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.cn.dao.OrderDao;
import com.cn.domain.Cart;
import com.cn.domain.News;
import com.cn.domain.Order;
import com.cn.domain.Product;
import com.cn.template.JDBCTemplate;
import com.cn.template.PreparedStatementSetter;
import com.cn.utils.DBUtils;

public class OrderDaoImpl extends JDBCTemplate implements OrderDao {
	
	//插入购物信息到购物车
	public int addOrder(final Cart cart,Connection conn) throws Exception{
		String sql="insert into HWUA_ORDER values(SEQ_ORDER.nextval,?,?,?,sysdate,?,1,1)";
		
		System.out.println(cart.toString());
		return this.update(conn, sql,new PreparedStatementSetter() {
			
			@Override
			public void setValues(PreparedStatement ps) throws SQLException {
				ps.setLong(1,cart.getUserid());
				ps.setString(2,cart.getUser().getHu_user_name());
				ps.setString(3, cart.getUser().getHu_address());
				ps.setDouble(4,cart.getQuantity()*cart.getProduct().getHp_price());
			}
		});
	}
	
	
	
	//插入购物信息到购物车
	public int addDatiles(final Cart cart,Connection conn) throws Exception{
		String sql="insert into HWUA_ORDER_DETAIL values(SEQ_DETAIL.nextval,?,?,?,?)";
		return this.update(conn, sql,new PreparedStatementSetter() {
			@Override
			public void setValues(PreparedStatement ps) throws SQLException {
				ps.setLong(1,cart.getOrder().getHo_id());
				ps.setLong(2,cart.getProduct().getHp_id());
				ps.setLong(3, cart.getQuantity());
				ps.setDouble(4,cart.getQuantity()*cart.getProduct().getHp_price());
			}
		});
	}
	
	//根据所有的购物表中的信息显示到订单页面
	public List<Order> queryAllOrder(long id){
		List<Order> orderList=new ArrayList();
		try {
			Object[] params= {id};
			orderList=DBUtils.getQuryRunner().query("select * from HWUA_ORDER where HO_USER_ID=?",new BeanListHandler<Order>(Order.class),params);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return orderList;
	}
	//根据商品ID查询
	public Cart queryOrderById(String id){
		return null;	
	}
	
	//根据UserID查询记录订单号
	@Override
	public List<Order> queryOrdersPage(long userId,int pageNow,int pageSize){
		List<Order> list=new ArrayList();
		try {
			int num=(pageNow-1)*pageSize;
			int end=pageNow*pageSize; 
			String sql="select * from (select t1.*,rownum rn from (select * from HWUA_ORDER where HO_USER_ID="+userId+" order by HO_CREATE_TIME desc) t1 where rownum<="+end+") where rn>"+num+"";
			list=DBUtils.getQuryRunner().query(sql,new BeanListHandler<Order>(Order.class));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
		
	//获取总记录数
	@Override
	public int queryOrdersByUserId(long userId){
		List<Order> list=new ArrayList();
		try {
			String sql="select * from HWUA_ORDER where HO_USER_ID="+userId+"";
			list=DBUtils.getQuryRunner().query(sql,new BeanListHandler<Order>(Order.class));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list.size();
	}


	//取出订单最新插入的一条记录
	@Override
	public Order queryCartByTime() {
		QueryRunner qr=new QueryRunner();
		String sql="select t.*,rownum from (select * from HWUA_ORDER order by HO_CREATE_TIME desc) t where rownum<=1";
		Order order=null;
		try {
			order=DBUtils.getQuryRunner().query(sql,new BeanHandler<Order>(Order.class));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return order;
	}
}